package com.dengyiming.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dengyiming.common.client.AddressClient;
import com.dengyiming.common.client.ItemClient;
import com.dengyiming.common.dto.Address;
import com.dengyiming.common.dto.Item;
import com.dengyiming.order.mapper.OrderDetailMapper;
import com.dengyiming.order.mapper.OrderLogisticsMapper;
import com.dengyiming.order.pojo.Order;
import com.dengyiming.order.mapper.OrderMapper;
import com.dengyiming.order.pojo.OrderDTO;
import com.dengyiming.order.pojo.OrderDetail;
import com.dengyiming.order.pojo.OrderLogistics;
import com.dengyiming.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private AddressClient addressClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderLogisticsMapper orderLogMapper;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Override
    public void orderInsert(OrderDTO orderDTO) {
        Item item = itemClient.itemById(orderDTO.getItemId());


        Order order = new Order();
        order.setId(System.currentTimeMillis());
        order.setTotalFee(item.getPrice()*orderDTO.getNum());
        order.setPaymentType(orderDTO.getPaymentType());
        order.setUserId(2L);
        order.setStatus(1);
        orderMapper.insert(order);
        Long orderId = order.getId();
        OrderDetail orderDetail = new OrderDetail(item);
        orderDetail.setNum(orderDTO.getNum());
        orderDetail.setOrderId(orderId);
        //插入商品详情到商品详情表
        orderDetailMapper.insert(orderDetail);
        Long addressId = orderDTO.getAddressId();
        Address address = addressClient.findAddressById(addressId);
        OrderLogistics orderLog = new OrderLogistics(address);
        orderLog.setOrderId(orderId);
        orderLogMapper.insert(orderLog);

    }
}
